package com.cyl.game.demos.web.controller;
import com.cyl.game.demos.web.entity.Result;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.cyl.game.demos.web.entity.Hero;
import com.cyl.game.demos.web.entity.User;
import com.cyl.game.demos.web.service.UserService;

@RestController
@RequestMapping("/users")
public class UserController {
     @Resource
    private UserService userService;

    // 注册新用户
    @PostMapping("/register")
    public Result registerUser(@RequestBody User user) {
        int result = userService.addUser(user);
        if (result > 0) {
            return Result.success("用户注册成功") ;
        } else {
            return Result.failure(204,"用户注册失败");
        }
    }

    // 根据用户名查询用户信息
    @GetMapping("/{username}")
    public Result<User> getUserByUsername(@PathVariable("username") String username) {
        return Result.success("成功",userService.getUserByUsername(username));
    }

    // 查询所有用户信息
    @GetMapping("/all")
    public Result<List<User>> getAllUsers() {
        return Result.success(userService.getAllUsers());
    }
        // 根据用户 ID 获取该用户所拥有的英雄 ID 列表
    @GetMapping("/{user_id}/hero-ids")
    public Result<List<Integer>> getHeroIdsByUserId(@PathVariable("user_id") Integer user_id) {
        return Result.success(userService.getHeroIdsByUserId(user_id));
    }

    // 获取所有用户及其所拥有的英雄 ID 列表
    @GetMapping("/all-with-hero-ids")
    public Result<Map<Integer, List<Integer>>> getAllUsersWithHeroIds() {
        return Result.success(userService.getAllUsersWithHeroIds());
    }
     // 让指定用户拥有指定英雄
    @PostMapping("/{user_id}/heroes/{hero_id}")
    public Result addHeroForUser(@PathVariable("user_id") int user_id, @PathVariable("hero_id") int hero_id) {
        userService.addHeroForUser(user_id, hero_id);
        return Result.success("成功让用户拥有该英雄");
    }

    // 移除指定用户对指定英雄的拥有关系
    @DeleteMapping("/{user_id}/heroes/{hero_id}")
    public Result removeHeroFromUser(@PathVariable("user_id") int user_id, @PathVariable("hero_id") int hero_id) {
        userService.removeHeroFromUser(user_id, hero_id);
        return Result.success("成功移除用户对该英雄的拥有关系");
    }

    // 获取指定用户所拥有的所有英雄设计信息
    @GetMapping("/{user_id}/heroes")
    public Result<List<Hero>> getHeroesOwnedByUserId(@PathVariable("user_id") int user_id) {
        return Result.success(userService.getHeroesOwnedByUserId(user_id));
    }

    // 获取拥有指定英雄的所有用户信息
    @GetMapping("/heroes/{hero_id}/owners")
    public Result<List<User>> getUsersWhoOwnHero(@PathVariable("hero_id") int hero_id) {
        return Result.success(userService.getUsersWhoOwnHero(hero_id));
    }
}